package com.fudan.adse.controller;

import com.fudan.adse.api.CommonResult;
import com.fudan.adse.domain.dto.NewTopUpDTO;
import com.fudan.adse.domain.po.TopUpOrder;
import com.fudan.adse.service.TopUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.fudan.adse.domain.cont.OrderStatus.CANCEL;
import static com.fudan.adse.domain.cont.OrderStatus.PAID;

/**
 * 充值管理控制器
 *
 * @author Jerry Zhang
 * create: 2022-11-23 22:50
 */
@RestController
@RequestMapping("/api/v1/tms/outer")
public class TopUpOrderController {
    @Autowired
    private TopUpService topUpService;

    /**
     * 充值
     *
     * @param newTopUp
     * @return
     */
    @PostMapping(value = "/topUpOrder")
    public CommonResult<TopUpOrder> topUp(@RequestBody NewTopUpDTO newTopUp) {
        return CommonResult.success(topUpService.topUp(newTopUp));
    }


    /**
     * 支付订单
     *
     * @param uuid
     * @return
     */
    @PutMapping(value = "/topUpOrder/paid")
    public CommonResult paid(@RequestParam("uuid") String uuid) {
        topUpService.updateOrder(uuid, PAID.getStatus());
        return CommonResult.success(null);
    }

    /**
     * 取消订单
     *
     * @param uuid
     * @return
     */
    @PutMapping(value = "/topUpOrder/refund")
    public CommonResult refund(@RequestParam("uuid") String uuid) {
        topUpService.updateOrder(uuid, CANCEL.getStatus());
        return CommonResult.success(null);
    }

    /**
     * 获取某人的充值订单
     *
     * @param username
     * @param status   订单状态：未支付、已支付、已取消
     * @return
     */
    @GetMapping("/topUpOrders")
    public CommonResult<List<TopUpOrder>> getOrdersByUsername(@RequestParam(value = "username", required = false) String username,
                                                              @RequestParam(value = "status", required = false) String status) {
        return CommonResult.success(topUpService.getOrdersByConditions(username, status));
    }

}
